<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of compras_model
 *
 * @author Neil
 */
class compras_model extends CI_Model {

    public function listadoCompras() {
        $sql = "call sp_listarComprobanteCompra()";
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $fila) :
                $data[] = $fila;
            endforeach;
        }else {
            $data = null;
        }
        return $data;
    }
    
    public function IU_ComprobanteCompra($params, $idcompras, $detalle) {
        $this->db->trans_start();
        $sql = "call sp_IU_ComprobanteCompra(?,?,?,?,?,?,?,?,?,?,@id)";
        $this->db->query("select '$idcompras' into @id");
        $this->db->query($sql, $params);
        $query = $this->db->query("select @id as codComp");
        $codComp = $query->row()->codComp;
        $sql1 = "call sp_I_detalleCompra(?,?,?,?,?)";
        foreach ($detalle as $fila) {
            $params2 = array($codComp, $fila->codProd, $fila->cant, $fila->pre, $fila->preTot);
            $this->db->query($sql1, $params2);
        }
        $this->db->trans_complete();
        if ($this->db->trans_status() == FALSE) {
            $msg = 0;
        } else {
            $msg = 1;
        }
        return $msg;
    }

}

?>
